//
//  PKIMViewController.swift
//  Panku
//
//  Created by mac on 2017/2/13.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit

private let PKIMViewControllerCellID = "PKIMViewControllerCellID"

class PKIMViewController: PKBaseViewController ,UITableViewDataSource,UITableViewDelegate{

    //MARK: - init
    init(viewModel:PKIMViewModel){
        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - properties
    //UI
    lazy var tableView = PKBaseTableView { (make) in
        
    }
    
    //viewMode;
    var viewModel:PKIMViewModel?
    
    //MARK: - life circle
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    
    //MARK: - UI
    override func setupRootView() {
        
    }
    
    override func setupSubViews() {
        view.addSubview(tableView)
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(PKIMViewControllerCell.self, forCellReuseIdentifier: PKIMViewControllerCellID)
        
    }
    
    override func setupSubViewsConstraints() {
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
    }
    
    override func setupNavigationBar() {
        
        //title文字
        self.title = "消息"
        
        //忽略未读 按钮
        let ignoreButton = PKBaseButton.createButton()
        ignoreButton.frame = CGRect(x: 0, y: 0, width: 100, height: 44)
        ignoreButton.setTitleColor(UIColor.darkGray, for: .normal)
        ignoreButton.addBlock(for: .touchUpInside) { (_) in
            //TODO:忽略消息操作
        }
        ignoreButton.contentHorizontalAlignment = .right
        ignoreButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        ignoreButton.setTitle("忽略未读", for: .normal)
        let ignoreBarButtonItem = UIBarButtonItem(customView: ignoreButton)
        self.navigationItem.rightBarButtonItem = ignoreBarButtonItem
        
    }
    
    override func bindViewModel() {
        
    }

}

extension PKIMViewController{
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel!.numberOfSections()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel!.numberOfRows()

    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: PKIMViewControllerCellID, for: indexPath)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //TODO:消息被点击后
    }
}
